<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

// 为方便系统核心升级，二次开发中需要用到的公共函数请写在这个文件，不要去修改common.php文件

if (!function_exists('hex2binarystring')) {
    /**
     * 十六进制数字字符串转二进制数字字符串
     * @param string $hexstring 需要转换的字符串
     * @param string $out_delimiter 二进制输出分隔符
     * @return bool|string
     */
    function hex2binarystring($hexstring = '', $out_delimiter = '')
    {
        $hexstring = strtolower(trim($hexstring));
        if (empty($hexstring)) return false;
        if (!empty(str_replace(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'], '', $hexstring))) return false;

        $hexstring = str_split($hexstring);

        $binaryString = '';
        for ($i = 0; $i < count($hexstring); $i++) {
            $binaryString .= str_pad(base_convert($hexstring[$i], 16, 2), 4, '0', STR_PAD_LEFT);
            if ($i % 2 != 0) $binaryString .= $out_delimiter;
        }
        $binaryString = empty($out_delimiter) ? $binaryString : trim($binaryString, $out_delimiter);

        return $binaryString;
    }
}


if (!function_exists('bin2chr')) {
    /**
     * 二进制数字字符串转字符串
     * @param string $binaryString 需要转换的字符串
     * @param string $bin_delimiter 二进制分隔符
     * @return bool|false|string
     */
    function bin2chr($binaryString = '', $bin_delimiter = '')
    {
        $binaryString = trim($binaryString);
        if (empty($binaryString)) return false;

        if (!empty($bin_delimiter)) {
            if (false === strpos($binaryString, $bin_delimiter)) return false;

            if (!empty(str_replace(['1', '0', $bin_delimiter], '', $binaryString))) return false;

            $binaryString = explode($bin_delimiter, $binaryString);
        } else {
            if (!empty(str_replace(['1', '0'], '', $binaryString))) return false;

            $binaryString = str_split($binaryString, 8);
        }

        $hexString = '';
        for ($i = 0; $i < count($binaryString); $i++) {
            $hexString .= str_pad(base_convert($binaryString[$i], 2, 16), 2, '0', STR_PAD_LEFT);
        }

        return hex2bin($hexString);
    }
}

if (!function_exists('bin2hexstr')) {
    /**
     * 二进制转16进制字符串
     * @param string $binaryString 需要转换的字符串
     * @return bool|false|string
     */
    function bin2hexstr($binaryString = '', $out_delimiter = '')
    {
        if (empty($binaryString)) return false;

        $binaryString = str_split($binaryString);

        $hexString = '';
        for ($i = 0; $i < count($binaryString); $i++) {
            if (strlen(bin2hex($binaryString[$i])) == 1)
                $hexString .= '0'.bin2hex($binaryString[$i]) . $out_delimiter ?: '';;
            $hexString .= bin2hex($binaryString[$i]) . $out_delimiter ?: '';
        }
        return empty($out_delimiter) ? $hexString : trim($hexString, $out_delimiter);
    }
}